package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.EducationUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Education;
import com.ruoyi.system.service.IEducationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 课程Controller
 * 
 * @author ruoyi
 * @date 2021-08-06
 */
@RestController
@RequestMapping("/system/education")
public class EducationController extends BaseController
{
    @Autowired
    private IEducationService educationService;

    /**
     * 查询课程列表
     */
//    @PreAuthorize("@ss.hasPermi('system:education:list')")
    @GetMapping("/list")
    public TableDataInfo list(Education education)
    {
        startPage();
        List<Education> list = educationService.selectEducationList(education);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
//    @PreAuthorize("@ss.hasPermi('system:education:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Education education)
    {
        List<Education> list = educationService.selectEducationList(education);
        ExcelUtil<Education> util = new ExcelUtil<Education>(Education.class);
        return util.exportExcel(list, "education");
    }

    /**
     * 获取课程详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:education:query')")
    @GetMapping(value = "/{eId}")
    public AjaxResult getInfo(@PathVariable("eId") Long eId)
    {
        return AjaxResult.success(educationService.selectEducationById(eId));
    }

    /**
     * 新增课程
     */
//    @PreAuthorize("@ss.hasPermi('system:education:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(Education education, MultipartFile video,MultipartFile picture)
    {
        educationService.insertEducation(education);
        if(null!=video){
            try {
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = video.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//video//" + education.geteId());
                //具体文件
                Path path = Paths.get(profile + "//video//" + education.geteId() + "//" + video.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    file1.delete();
                }
                Files.write(path, bytes);//创建文件
                education.seteVideo("profile/video/" + education.geteId() + "/" + video.getOriginalFilename());

            } catch (IOException e) {
            }
        }
        if(null!=picture){
            try {
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = picture.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//picture//" + education.geteId());
                //具体文件
                Path path = Paths.get(profile + "//picture//" + education.geteId() + "//" + picture.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    file1.delete();
                }
                Files.write(path, bytes);//创建文件
                education.setePicture("profile/picture/" + education.geteId() + "/" + picture.getOriginalFilename());

            } catch (IOException e) {
            }
        }
        return toAjax(educationService.updateEducation(education));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "【学习】", businessType = BusinessType.INSERT)
    @PostMapping("/educationUser")
    public AjaxResult add(@RequestBody EducationUser educationUser)
    {
        if(educationService.selectEducationUserList(educationUser).size()==0){
            return toAjax(educationService.insertEducationUser(educationUser));
        }else {
            return AjaxResult.error("不能重复学习");
        }
    }
    @Log(title = "【是否学习】")
    @GetMapping("/User")
    public TableDataInfo list(EducationUser educationUser)
    {
        List<EducationUser> educationUsers = educationService.selectEducationUserList(educationUser);
        return getDataTable(educationUsers);
    }
    /**
     * 修改课程
     */
//    @PreAuthorize("@ss.hasPermi('system:education:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(Education education, MultipartFile video,MultipartFile picture)
    {
        if(null!=video){
            try {
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = video.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//video//" + education.geteId());
                //具体文件
                Path path = Paths.get(profile + "//video//" + education.geteId() + "//" + video.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    file1.delete();
                }
                Files.write(path, bytes);//创建文件
                education.seteVideo("profile/video/" + education.geteId() + "/" + video.getOriginalFilename());

            } catch (IOException e) {
            }
        }
        if(null!=picture){
            try {
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = picture.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//picture//" + education.geteId());
                //具体文件
                Path path = Paths.get(profile + "//picture//" + education.geteId() + "//" + picture.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    file1.delete();
                }
                Files.write(path, bytes);//创建文件
                education.setePicture("profile/picture/" + education.geteId() + "/" + picture.getOriginalFilename());

            } catch (IOException e) {
            }
        }
        return toAjax(educationService.updateEducation(education));
    }

    /**
     * 删除课程
     */
//    @PreAuthorize("@ss.hasPermi('system:education:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{eIds}")
    public AjaxResult remove(@PathVariable Long[] eIds)
    {
        return toAjax(educationService.deleteEducationByIds(eIds));
    }
}
